﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WaveringLand_Launcher.Helper.Net;
using WaveringLand_Launcher.Model;
using WaveringLand_Launcher.ViewModel.Component;

namespace WaveringLand_Launcher.Helper
{
    public class WlTaskHelper
    {
        public async static Task<List<WllFile>> GetLatestIndexAsync()
        {
            //WlTask wlt = new() { Path = "", Url = WlApiHelper.indexurl};
            return await WlApiHelper.GetFileIndexAsync();
        }

        public static Task CreateDownloadTask(ResFileViewModel resFile, Action completedAction = null)
        {
            resFile.IsDownloading = true;
            var t = Task.Factory.StartNew(() => {
                var c = new WebClient();
                c.DownloadFileCompleted += delegate (object sender, System.ComponentModel.AsyncCompletedEventArgs e) {
                    resFile.IsDownloading = false;
                    resFile.NeedUpdate = false;
                    if (completedAction != null)
                    {
                        completedAction.Invoke();
                    }

                };
                c.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e)
                {
                    resFile.DownloadProgress = e.ProgressPercentage;
                };
                c.DownloadFileTaskAsync(resFile.Url, resFile.Path);
            });
            return t;
        }




    }
}
